home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-10-04 | 20.2 KB | 611 lines |
-
- package sub_arctic.lib;
-
- import sub_arctic.lib.manager;
- import sub_arctic.output.drawable;
- import sub_arctic.input.pressable;
- import sub_arctic.input.move_draggable;
- import sub_arctic.input.grow_draggable;
- import sub_arctic.input.event;
- import sub_arctic.input.pick_collector;
- import sub_arctic.input.std_drag_filters;
- import java.awt.Point;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.FontMetrics;
-
- /**
- * Class to implement dragging and drawing for a semantic lens. When this
- * object is drawn, it does a traversal of the subtree rooted at the parent
- * of this object. That traversal does an alternate rendering (semantic
- * redraw) of the subtree within the bounds of this object. This redraw
- * can be done on top of the normal draw (by setting clear_back false, which
- * is the default), or the normal drawing can be cleared and replaced
- * the alternate rendering. Alternate renderings are done via a
- * "interactor_predicate" object that parameterizes this object and is passed
- * to a traverse_and_collect() of the parent.<p>
- *
- * This object provides a title bar (with an optional text title) that can be
- * used to move the lens, as well as a small grow handle to resize it. For
- * complete operation, this object should be placed under a
- * semantic_lens_parent object (the object will work with other parents, but
- * redraw anomalies for dynamically moving objects within the lens can occur).
- *
- * @see semantic_lens_parent
- * @see sem_draw_context
- * @see sem_draw_continue
- * @see base_interactor#traverse_and_collect
- * @author Scott Hudson
- */
- public class semantic_lens
- extends base_interactor implements pressable, move_draggable, grow_draggable {
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** unique identifier for sem_draw traversals */
- protected static final int sem_draw_trav_id = manager.unique_int();
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Indication of whether we clear our background first, or draw over the
- * top of the objects that were drawn before us.
- */
- protected boolean _clear_back = false;
-
- /**
- * Indication of whether we clear our background first, or draw over the
- * top of the objects that were drawn before us.
- * @return boolean indicating whether we clear our background.
- */
- public boolean clear_back() {return _clear_back;}
-
- /**
- * Set whether we clear our background first, or draw over the
- * top of the objects that were drawn before us.
- * @param boolean v indicating whether we should clear our background.
- */
- public void set_clear_back(boolean v)
- {
- _clear_back = v;
- damage_self();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * The "predicate" object that does the specialized drawing for this
- * lens. This object must be expecting a sem_draw_context object for
- * a parameter, and should normally return false in all cases.
- *
- * @see sem_draw_context
- */
- protected interactor_pred _draw_obj = null;
-
- /**
- * The "predicate" object that does the specialized drawing for this
- * lens. This object must be expecting a sem_draw_context object for
- * a parameter, and should normally return false in all cases.
- *
- * @see sem_draw_context
- * @return interactor_pred the current predicate.
- */
- public interactor_pred draw_obj() {return _draw_obj;}
-
- /**
- * Set the "predicate" object that does the specialized drawing for this
- * lens. This object must be expecting a sem_draw_context object for
- * a parameter, and should normally return false in all cases.
- *
- * @param interactor_pred dobj the drawing "predicate" object.
- */
- public void set_draw_obj(interactor_pred dobj)
- {
- _draw_obj = dobj;
- damage_self();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Title string */
- protected String _title_str = " ";
-
- /**
- * Title string.
- * @return String the current title string.
- */
- public String title_str() {return _title_str;}
-
- /**
- * Set title string.
- * @param String str the new title string.
- */
- public void set_title_str(String str)
- {
- _title_str = str;
- damage_self();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Color we do our title handle in. */
- protected Color _title_color = Color.gray;
-
- /**
- * Color we do our title handle in.
- * @return Color the color.
- */
- public Color title_color() {return _title_color;}
-
- /**
- * Set color we do our title handle in.
- * @param Color c the new color for out title handle.
- */
- public void set_title_color(Color c)
- {
- _title_color = c;
- damage_self();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Font we draw title handle in */
- protected Font _title_font = new Font("Helvetica",Font.PLAIN,9);
-
- /**
- * Font we draw title handle in.
- * @return Font the font.
- */
- public Font title_font() {return _title_font;}
-
- /**
- * Set font we draw title handle in.
- * @param Font fnt the new font to draw with.
- */
- public void set_title_font(Font fnt)
- {
- _title_font = fnt;
- metrics = manager.get_metrics(_title_font);
- damage_self();
- }
-
- /** FontMetrics object for the current font */
- protected FontMetrics metrics;
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Identifier for the particular kind of semantic redraw pass being done
- * here.
- */
- protected int _sem_draw_kind = manager.unique_int();
-
- /**
- * Identifier for the particular kind of semantic redraw pass being done
- * here.
- * @return int a unique identifier for this kind of redraw.
- */
- public int sem_draw_kind() {return _sem_draw_kind;}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Full constructor.
- *
- * @param int x x position of the lens
- * @param int y y position of the lens
- * @param int w width of the lens
- * @param int h height of the lens
- * @param interactor_pred draw_action a "predicate" object which does the
- * specialized drawing of an object for this lens
- * @param boolean clr_back do we clear the background before we draw
- * @param String ttl_str title string to be drawn on title drag bar
- * @param Color ttl_color color of title drag bar
- * @param Font ttl_font font to draw title in
- * @param int draw_kind identifier that indicates the kind of semantic
- * redraw that we are doing
- */
- public semantic_lens(
- int xv, int yv,
- int wv, int hv,
- interactor_pred draw_action,
- boolean clr_back,
- String ttl_str,
- Color ttl_color,
- Font ttl_font,
- int draw_kind)
- {
- super(xv,yv,wv,hv);
- set_draw_obj(draw_action);
- set_clear_back(clr_back);
- if (ttl_color != null)
- set_title_color(ttl_color);
- if (ttl_font != null)
- set_title_font(ttl_font);
- else
- set_title_font(_title_font);
- if (ttl_str != null)
- set_title_str(ttl_str);
- _sem_draw_kind = draw_kind;
- }
-
- //had:
- //* @exception general PROPAGATED
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Simple constructor with default font and title bar color.
- *
- * @param int x x position of the lens
- * @param int y y position of the lens
- * @param int w width of the lens
- * @param int h height of the lens
- * @param interactor_pred draw_action a "predicate" object which does the
- * specialized drawing of an object for this lens
- * @param boolean clr_back do we clear the background before we draw
- * @param int draw_kind identifier that indicates the kind of semantic
- * redraw that we are doing
- */
- public semantic_lens(
- int xv, int yv,
- int wv, int hv,
- interactor_pred draw_action,
- boolean clr_back,
- int draw_kind)
- {
- this (xv,yv,wv,hv,draw_action, clr_back, null, null, null, draw_kind);
- }
-
- //had:
- //* @exception general PROPAGATED
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Simplest constructor. Defaults to 0,0 : 100x100 with non-cleared
- * background.
- *
- * @param interactor_pred draw_action a "predicate" object which does the
- * specialized drawing of an object for this lens
- * @param int draw_kind identifier that indicates the kind of semantic
- * redraw that we are doing
- */
- public semantic_lens(interactor_pred draw_action, int draw_kind)
- {
- this(0,0,100,100,draw_action,false,draw_kind);
- }
-
- //had:
- //* @exception general PROPAGATED
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Override pick to just work on handles and let center of lens pass through.
- *
- * @param int pt_x x coordinate of picking point
- * @param int pt_y y coordinate of picking point
- * @param pick_collector pick_list result list
- */
- public void pick(int pt_x, int pt_y, pick_collector pick_list)
- {
- int title_h;
-
- /* if it doesn't hit our bound we're done */
- if (!inside_bounds(pt_x, pt_y)) return;
-
- /* see if it hits title handle */
- title_h = metrics.getHeight()+2;
- if (pt_y < title_h)
- {
- pick_list.report_pick(this, new Integer(0));
- return;
- }
-
- /* see if it hits the size handle */
- if (pt_x >= w()-10 && pt_y >= h()-10)
- {
- pick_list.report_pick(this, new Integer(1));
- return;
- }
- }
-
- //had:
- //* @exception general PROPAGATED
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Handle mouse button press input to the object by making us either the
- * move-drag or grow-drag focus.
- *
- * @param event evt the press event.
- * @param Object user_info the information associated with this object
- * at pick time.
- * @return boolean indicating whether the event was consumed.
- */
- public boolean press(event evt, Object user_info)
- {
- int which = ((Integer)user_info).intValue();
- if (which == 0) // title bar
- {
- /* make us the move-drag focus */
- manager.move_drag_focus.set_focus_to(this, evt, null);
- return true;
- }
- else if (which == 1) // grow handle
- {
- /* make us the grow-drag focus */
- manager.grow_drag_focus.set_focus_to(this, evt, null);
- return true;
- }
-
- /* shouldn't happen, but we have to keep the compiler happy */
- return false;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Companion to press -- here we ignore the releases.
- * @param event evt the release event.
- * @param Object user_info information associated with this object at
- * pick time.
- * @return boolean indicating whether we consumed this input (here always
- * false).
- */
- public boolean release(event evt, Object user_info)
- {
- return false;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Handle the start of a move-drag to the object.
- *
- * @param event evt the event "causing" the drag.
- * @param int xv the x location of the object at start of drag (in
- * parent's coordinates).
- * @param int yv the y location of the object at start of drag (in
- * parent's coordinates).
- * @param int gx initial grab location within the object.
- * @param int gy initial grab location within the object.
- * @param Object user_info the information associated with this object when
- * it requested drag focus.
- * @return boolean indicating whether the input was consumed.
- */
- public boolean drag_start(
- event evt, int xv, int yv, int gx, int gy, Object user_info)
- {
- /* move our position */
- set_pos(xv,yv);
- return true;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Handle a movement during a move-drag. Here we just set our position to
- * follow the event location.
- *
- * @param event evt the event "causing" the drag.
- * @param int x_pos the x position this object should move to.
- * @param int y_pos the y position this object should move to.
- * @param int start_x the x location of the object at start of drag (in
- * parent's coordinates).
- * @param int start_x the y location of the object at start of drag (in
- * parent's coordinates).
- * @param int grab_x initial grab location within the object.
- * @param int grab_y initial grab location within the object.
- * @param Object user_info the information associated with this object when
- * it requested drag focus.
- * @return boolean indicating whether the input was consumed.
- */
- public boolean drag_feedback(
- event evt,
- int x_pos, int y_pos,
- int start_x, int start_y,
- int grab_x, int grab_y,
- Object user_info)
- {
-
- /* move our position */
- set_pos(x_pos, y_pos);
- return true;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Handle input corresponding to the end of a move-drag. In addition to
- * normal dragging, we force the object back inside its parent if it is
- * too far outside.
- *
- * @param event evt the event "causing" the drag.
- * @param int x_pos the x position this object should move to.
- * @param int y_pos the y position this object should move to.
- * @param int start_x the x location of the object at start of drag (in
- * parent's coordinates).
- * @param int start_x the y location of the object at start of drag (in
- * parent's coordinates).
- * @param int grab_x initial grab location within the object.
- * @param int grab_y initial grab location within the object.
- * @param Object user_info the information associated with this object when
- * it requested drag focus.
- * @return boolean indicating whether the input was consumed.
- */
- public boolean drag_end(
- event evt,
- int x_pos, int y_pos,
- int start_x, int start_y,
- int grab_x, int grab_y,
- Object user_info)
- {
- boolean result;
-
- /* let drag_feedback to all the drag work */
- result=drag_feedback(evt, x_pos, y_pos, start_x, start_y, grab_x, grab_y,
- user_info);
-
- /* force us back inside our parent if we ended up out */
- if (x() < 10-w()) set_x(10-w());
- if (y() < 0) set_y(0);
- if (x() > parent().w()-10) set_x(parent().w()-10);
- if (y() > parent().h()-10) set_y(parent().h()-10);
-
- return result;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Handle the start of a grow-drag to the object.
- *
- * @param event evt the event "causing" the drag.
- * @param Object user_info the information associated with this object at
- * the point that it requested focus.
- * @return boolean indicating whether the input was consumed.
- */
- public boolean drag_start(event evt, Object user_info)
- {
- /* nothing to do here */
- return true;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Handle a movement during a grow-drag. Here we just set our size to
- * follow the drag, but don't go smaller than 30x30.
- *
- * @param event evt the event "causing" the drag.
- * @param int cur_w the current width indicated by the drag.
- * @param int cur_h the current height indicated by the drag.
- * @param int st_w the starting width of this object.
- * @param int st_w the starting height of this object.
- * @param Object user_info the information associated with this object at
- * the point that it requested focus.
- * @return boolean indicating whether the input was consumed.
- */
- public boolean drag_feedback(
- event evt,
- int cur_w, int cur_h,
- int st_w, int st_h,
- Object user_info)
- {
- /* don't go smaller than 30x30 */
- if (cur_w < 30) cur_w = 30;
- if (cur_h < 30) cur_h = 30;
-
- /* set our size */
- set_size(cur_w, cur_h);
- return true;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Handle input corresponding to the end of a grow-drag.
- *
- * @param event evt the event "causing" the drag.
- * @param int cur_w the current width indicated by the drag.
- * @param int cur_h the current height indicated by the drag.
- * @param int st_w the starting width of this object.
- * @param int st_w the starting height of this object.
- * @param Object user_info the information associated with this object at
- * the point that it requested focus.
- * @return boolean indicating whether the input was consumed.
- */
- public boolean drag_end(
- event evt,
- int cur_w, int cur_h,
- int st_w, int st_h,
- Object user_info)
- {
- /* let drag_feedback to all the work */
- return drag_feedback(evt, cur_w, cur_h, st_w, st_h, user_info);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Override draw_self to do a semantic lens. This works by going up one
- * level to the parent, then doing a sem_draw traversal from there down.
- * the sem_draw traversal draws a semantically modified rendition of the
- * sub-tree on our drawable.<p>
- *
- * Note, this drawing is done inside our bounds, but the drawing
- * corresponding to each individual object found in the traversal starting
- * from our parent is not limited to their bounds. This requires special
- * processing in damage_self within our parent.
- *
- * @see semantic_draw_parent
- * @see sem_draw_context
- * @see sem_draw_continue
- * @see sem_draw_to_child
- * @see base_interactor#traverse_and_collect
- * @param drawable d the drawing surface we draw on.
- */
- protected void draw_self_local(drawable d)
- {
- drawable dcopy;
- sem_draw_context context;
- int hv;
-
- /* clear our background if needed */
- if (clear_back())
- {
- d.setColor(Color.white);
- d.fillRect(0,0,w()-1, h()-1);
- }
- d.setColor(Color.black);
-
- /* make a copy of our drawable and put it in our parent's coords */
- dcopy = d.copy();
- dcopy.translate(-x(), -y());
-
- /* make a sem_draw_context out of that */
- context = new sem_draw_context(sem_draw_kind(), dcopy);
-
- /* do the redraw traversal starting at our parent */
- parent().traverse_and_collect(sem_draw_trav_id, TRAV_DRAW, draw_obj(),
- new sem_draw_continue(), new sem_draw_to_child(), context,
- new pick_collector());
- d.drawRect(0,0,w()-1, h()-1);
-
- /* draw our title bar handle */
- hv = metrics.getHeight()+4;
- d.setColor(title_color());
- d.fillRect(0,0, w()-1, hv);
- d.setColor(Color.white);
- d.setFont(title_font());
- d.drawString(title_str(), 2, metrics.getAscent()+2);
-
- /* draw our grow handle */
- d.setColor(title_color());
- d.fillRect(w()-10, h()-10, 10,10);
-
- /* draw a rectangle over the top */
- d.setColor(Color.black);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
- }
- /*=========================== COPYRIGHT NOTICE ===========================
-
- This file is part of the subArctic user interface toolkit.
-
- Copyright (c) 1996 Scott Hudson and Ian Smith
- All rights reserved.
-
- The subArctic system is freely available for most uses under the terms
- and conditions described in
- http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html
- and appearing in full in the lib/interactor.java source file.
-
- The current release and additional information about this software can be
- found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
-
- ========================================================================*/
-